package br.com.felix.dbm.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import br.com.felix.dbm.controller.DataModelController;
import br.com.felix.dbm.database.Table;

public class EditorSideBar extends JPanel{
	
	
	
	private static final long serialVersionUID = 3179096422059593609L;
	private DataModelController dmc;
	private EditorSideBarDelegate editorSideBarDelegate;
	
	public EditorSideBar(DataModelController dmc){
		this.dmc = dmc;
		configureDefaults();
	}

	private void configureDefaults() {
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		JCheckBox chkVisible;
		JButton btnRelated;
		for(final Table table: dmc.getDataModel().getTables()){
			chkVisible = new JCheckBox(table.getName(), true);
			chkVisible.addChangeListener(new ChangeListener() {
				@Override
				public void stateChanged(ChangeEvent e) {
					if(editorSideBarDelegate!=null){
						editorSideBarDelegate.tableSelected(table, ((JCheckBox)e.getSource()).isSelected());
					}
				}
			});
			chkVisible.setFont(Style.getTextFont());
			btnRelated = new JButton("*");
			btnRelated.setPreferredSize(Style.getReallySmallButtonSize());
			btnRelated.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent e) {
					if(editorSideBarDelegate!=null){
						editorSideBarDelegate.tableNeighborsSelected(table);
					}
				}
			});
			gbc.gridx=0;
			add(chkVisible,gbc);
			gbc.gridx=1;
			add(btnRelated,gbc);
			gbc.gridy++;
		}		
	}

	public void setEditorSideBarDelegate(EditorSideBarDelegate editorSideBarDelegate) {
		this.editorSideBarDelegate = editorSideBarDelegate;
	}

}
